package com.cvtt.voice.simple;

public class Speex {
	
	private static Speex instance = null;
	
	private boolean isLoad = false;
	private boolean isInit = false;
	
	private boolean isReady = false;
	
	private int frameSize = 0;
	private int samRate = 0;
	
	public synchronized static Speex getInstance()
	{
		if(instance == null)
		{
			instance = new Speex();
		}
		
		return instance;
	}
	
	private Speex()
	{		
		load();	
	}
	
	private void load() {
		try {
			System.loadLibrary("speex");
			isLoad = true;
		} catch (Throwable e) {
			e.printStackTrace();
			isLoad = false;
		}
	}
	
	public boolean init(int frame_size, int sampling_rate)
	{
		frameSize = frame_size;
		samRate = sampling_rate;
		if(isInit)
			return true;
		if(isLoad == false)
			return false;
		if(initSpeex(frameSize, samRate) != 0)
			return false;
		isInit = true;
		isReady = true;
		return true;
	}
	
	public boolean isReady()
	{
		return isReady;
	}
	
	public synchronized void destroy()
	{
		if(isInit)
			destroySpeex();
		isInit = false;
		isReady = false;
	}
		
	private native int initSpeex(int frame_size, int sampling_rate);

	public native void destroySpeex();

	public native void doPlay(short play[],int off,int num);

	public native void doProcess(short[] rec,short out[],int pos,int num);
	
	public native void doAEC(short[] rec,int rec_pos,short play[],int play_pos,short out[],int num);

}
